using System;
using Game.Main.Model;
using Game.XH;
using UnityEngine;

namespace Game.Component
{
	public class SwingItem : BaseItem
	{
		private const float SHAKE_ANGLE = 10f;

		private bool isShake;

		private bool isContinueShake;

		private float curVal;

		private float curSinVal;

		private int nextIndex;

		private int stopAnimStage;

		private float shakeAngle;

		private Transform swing;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			swing = model.Find("Swing");
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (isShake)
			{
				if (isContinueShake)
				{
					curVal += Time.deltaTime * 1.5f * (10f / shakeAngle);
					if (curVal >= (float)Math.PI * 2f)
					{
						curVal = 0f;
						isContinueShake = false;
						shakeAngle = 10f;
					}
				}
				else
				{
					curVal += Time.deltaTime * 1.5f;
				}
				curSinVal = Mathf.Sin(curVal);
				swing.SetLocalRotationZ(curSinVal * shakeAngle);
			}
			else
			{
				if (stopAnimStage == 0)
				{
					return;
				}
				curVal += Time.deltaTime * 1.5f * (10f / shakeAngle);
				if (curVal >= (float)nextIndex * (float)Math.PI)
				{
					curVal = (float)nextIndex * (float)Math.PI;
				}
				curSinVal = Mathf.Sin(curVal);
				swing.SetLocalRotationZ(curSinVal * shakeAngle);
				if (curVal >= (float)nextIndex * (float)Math.PI - 1E-05f)
				{
					nextIndex++;
					if (shakeAngle > 2f)
					{
						shakeAngle -= 2f;
					}
					else
					{
						shakeAngle -= 1f;
					}
					if (shakeAngle <= 0f)
					{
						shakeAngle = 0f;
						stopAnimStage = 0;
					}
				}
			}
		}

		public override bool AttachItem(BaseItem item, DepthType depthType, AttachType attachType, bool updatePosition)
		{
			bool flag = base.AttachItem(item, depthType, attachType, updatePosition);
			if (flag)
			{
				isShake = true;
				stopAnimStage = 0;
				if (shakeAngle == 0f)
				{
					shakeAngle = 10f;
					curVal = 0f;
					isContinueShake = false;
				}
				else
				{
					curVal %= (float)Math.PI * 2f;
					isContinueShake = true;
				}
			}
			return flag;
		}

		public override void ReleaseAttachItem(BaseItem item)
		{
			base.ReleaseAttachItem(item);
			if (childItems.Count <= 0)
			{
				isShake = false;
				curVal %= (float)Math.PI * 2f;
				if (curVal >= (float)Math.PI)
				{
					nextIndex = 2;
				}
				else
				{
					nextIndex = 1;
				}
				stopAnimStage = 1;
			}
		}

		public override Transform GetSitPoint()
		{
			return swing.Find("SitPoint");
		}
	}
}
